package com.akk.platform.manage;

import com.akk.module.avs.entity.AvsPayAccess;
import com.akk.module.avs.model.response.AvsPayAccessRes;
import com.akk.module.avs.model.response.AvsPayAllAccessRes;
import com.akk.module.avs.service.AvsPayAccessService;
import com.akk.utils.IDUtils;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/platform/manage")
public class AvsPayAccessAction {

    @Autowired
    private AvsPayAccessService payAccessService;

    @RequestMapping(value = "/payAccessList")
    @RequiresPermissions("platform:manage:payAccess:list")
    public RestResponse list(@Valid Query query, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage());
        PageHelper.startPage(query.getPage(),query.getLimit());
        List<AvsPayAccessRes> list = payAccessService.accessList();
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    @RequestMapping(value = "/payAccessInfo/{payId}")
    @RequiresPermissions("platform:manage:payAccess:info")
    public RestResponse info(@PathVariable String payId){
        return RestResponse.build(payAccessService.queryById(payId));
    }

    @RequestMapping(value = "/payAccessUpdate",method = RequestMethod.POST)
    @RequiresPermissions("platform:manage:payAccess:update")
    public RestResponse update(@RequestBody AvsPayAccess payAccess){
        AvsPayAccess record = payAccessService.queryById(payAccess.getPayAccessId());
        if (record == null)
            return RestResponse.build("1","信息不存在");
        record.setAccessId(payAccess.getAccessId());
        record.setDeposit(payAccess.getDeposit());
        record.setRate(payAccess.getRate());
        record.setSingleCard(payAccess.getSingleCard());
        record.setSingleTime(payAccess.getSingleTime());
        record.setSingleUser(payAccess.getSingleUser());
        record.setTransEnd(payAccess.getTransEnd());
        record.setTransStart(payAccess.getTransStart());
        record.setPayStatus(payAccess.getPayStatus());
        record.setGatewayCost(payAccess.getGatewayCost());
        record.setClearMoney(payAccess.getClearMoney());
        record.setClearRate(payAccess.getClearRate());
        record.setMinMoney(payAccess.getMinMoney());
        payAccessService.update(record);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/payAccessAdd",method = RequestMethod.POST)
    @RequiresPermissions("platform:manage:payAccess:save")
    public RestResponse save(@RequestBody AvsPayAccess payAccess){
        payAccess.setPayAccessId(IDUtils.getPK());
        payAccessService.save(payAccess);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/payAllAccessList")
    @RequiresPermissions("platform:manage:payAccess:accessList")
    public RestResponse accessList(){
        List<AvsPayAllAccessRes> list = payAccessService.allAccess();
        return RestResponse.build(list);
    }


}
